<template>
	<view class="detail-page">
		<view class="coll-lay">
			<!-- <l-file ref="lFile"></l-file> -->
			<view class="coll-row" v-if="schData.isColl === 1">
				<text>{{schData.createName}}</text> {{schData.createTime}}
			</view>
			
			<view class="sch-time">
			<view class="sch-time-item">
				{{schData.schContent}}
			</view>
			<view class="file-list" v-if="schData.fileImgUrls.length>0">
				<template v-for="(vf, i) in schData.fileImgUrls">
					<image :src="vf.url" :key="i" mode="aspectFill" @load="loadImg(i)" @click="previewImage(i, schData.fileImgUrls)" ></image>
				</template>
			</view>
			<view class="file-list" v-if="schData.filePdfUrls.length>0">
				<view class="filesBox">
					<view class="fileslist" v-for="(item,index) in schData.filePdfUrls" :key="index">
						<uni-icons type="paperclip" size="20" class="upfileico"></uni-icons>
						<text class="upname rf-ellipsis" @click="onOpenDoc(item.url)">{{item.name}}</text>
					</view> 
				</view>
			</view>
			<view class="sch-time-item">
				<text>时间：</text>
				{{schData.startTime}} - 
					<text style="color:#000000" v-if="schData.endTime.length == 5">{{schData.endTime}}</text>
					<text style="color:#000000" v-else>{{schData.endTime}}</text>
				
			</view>
			<view class="sch-time-item"  style="border:0px;">
				<text>提醒：</text>{{txArray[schData.remindFlag - 0 + 1]}}
			</view>
			<view class="sch-time-item" v-if="schData.repeatFlag != 0" style="border:0px;">
				<text>重复：</text>{{cfArray[schData.repeatFlag]}}
			</view>
		</view>
		</view>
		<view class="done-bar" v-if="schData.status === 0 && schData.isStart">
			
			<button type="primary" class="finish_btn" v-if="schData.ownerName != schData.createName && schData.collFlag == 3" @click="doneClick">完成</button>
			<button type="primary" class="finish_btn" v-if="schData.ownerName == schData.createName" @click="summryClick">完成并总结</button>
		</view>
		
		<view class="summry-content" v-if="!!schData.schSummary">
			<view class="title">总结<text>{{$u.timeFormat(new Date(schData.schSummary.createTime), 'yyyy-mm-dd hh:MM')}}</text></view>
			<view class="content">{{schData.schSummary.schContent}}</view>
			<view class="file-list">
				<template v-for="(vf, i) in schData.schSummary.fileImgUrls">
					<image :src="vf.url" mode="aspectFill" :key="i" @click="previewImage(i, schData.schSummary.fileImgUrls)"></image>
				</template> 
			</view>
			<view class="file-list">
				<view class="filesBox">
					<template v-for="(item,index) in schData.schSummary.filePdfUrls">
						<view class="fileslist" :key="index">
							<uni-icons type="paperclip" size="20" class="upfileico"></uni-icons>
							<text class="upname rf-ellipsis" @click="onOpenDoc(item.url)">{{item.name}}</text>
						</view> 
					</template>
				</view>
			</view>
		</view>
		<view class="coll-list" v-if="schData.isColl === 1" @click="toCollList">
			<view class="title">协作人</view>
			<view class="rtext">
				{{jsCount}}/{{schData.schCollaborators.length}}人接受
				<uni-icons type="arrowright" size="18" color="#999999"></uni-icons>
			</view>
		</view>
		<view class="fk-list">
			<view class="title">最近反馈</view>
			<view class="fk-btn" @click="fkClick">我要反馈</view>
		</view>
		<view class="fk-lay">
			<view class="fk-item" v-for="(v, i) in schData.schFeedbackVos" :key="i">
				<view class="fk-from">
					{{v.feedBackerName}}<view class="fk-time">{{$u.timeFormat(new Date(v.createTime), 'yyyy-mm-dd hh:MM')}}</view>
				</view>
				<view class="fk-content">
					{{decodeURIComponent(v.schContent)}}
				</view>
				<view class="file-list" v-if="v.fileImgUrls.length>0">				
					<template v-for="(vf, i) in v.fileImgUrls">
						<image :src="vf.url" mode="aspectFill" :key="i" @click="previewImage(i, v.fileImgUrls)"></image>
					</template>
				</view>
				<view class="file-list" v-if="v.filePdfUrls.length>0">
					<view class="filesBox">
						<view class="fileslist" v-for="(item,index) in v.filePdfUrls" :key="index">
							<uni-icons type="paperclip" size="20" class="upfileico"></uni-icons>
							<text class="upname rf-ellipsis" @click="onOpenDoc(item.url)">{{item.name}}</text>
						</view> 
					</view>
				</view>
				<view class="fk-del">
					<text @click="fkDelClick(v.pkid)" v-if="v.feedBackerName === schData.ownerName">删除反馈</text>
				</view>
			</view>
			<view class="no-fk" v-if="schData.schFeedbackVos.length == 0">
				暂无反馈
			</view>
		</view>
		<view class="js-bar" v-if="schData.ownerName != schData.createName && schData.collFlag < 3 && schData.status === 0">
			<button class="js-btn" type="default" @click="jsClick(0)">不接受</button>
			<button class="js-btn" type="primary" @click="jsClick(1)">接受</button>
		</view>
	</view>
</template>

<script>
	import uniIcons from '@/components/uni-icons/uni-icons.vue'
	// import { detailSchedule, acceptOrRefuse, removeFeedBack, setDone, remove, addFeedBack } from '@/api/schedule/api.js'
	
	
	
	export default {
		components: { uniIcons },
		data() {
			return {
				pkid: '',
				schData: {
					createName: '',
					createTime: 0,
					startTime: '',
					endTime: '',
					schFeedbackVos: [],
					fileImgUrls:[],
					filePdfUrls:[]
				},
				jsCount: 0,
				txArray: [ '不提醒', '日程开始时', '提前5分钟', '提前15分钟', '提前30分钟', '提前一小时', '提前一天'],
				cfArray: ['一次性日程', '每工作日', '每日', '每周', '每两周', '每月', '每年']
			};
		},
		onLoad(option) {
			if (option.pkid) {
				this.pkid = option.pkid
				// this.initData()
			}
		},
		onShow() {
			this.initData()
		},
		onNavigationBarButtonTap() {
			let _menu_list = []
			let _isCreate = this.schData.ownerName == this.schData.createName
			let _done = this.schData.status === 0
			
			// if (_isCreate && _done) {
			// 	_menu_list.push('编辑')
			// }
			if (_isCreate) {
				if (_done) {
					_menu_list.push('编辑日程')
				}
				if (this.schData.repeatFlag != 0) {
					_menu_list.push('仅删除此日程')
					_menu_list.push('删除所有日程')
				} else {
					_menu_list.push('删除日程')
				}
			} else {
				_menu_list.push('删除日程')
			}
			
			uni.showActionSheet({
				itemList: _menu_list,
				success: (res) => {
					let _menuName = _menu_list[res.tapIndex]
					let _isRel = (_menuName === '仅删除此日程' || _menuName === '删除日程')
					
					if (_menuName.indexOf('删除') !== -1) {
						uni.showModal({
							title: '提示',
							content: `确定${_menuName}吗？`,
							success: (res) => {
								if (res.confirm) {
									let _obj = {
										pkid: this.pkid,
										isRel: _isRel?0:1
									}
									uni.showLoading();
									this.$u.api.scheduleRemove(_obj).then(res => {
										uni.hideLoading()
										this.$u.toast('删除成功')
										this.$u.route({type: 'back'})
									}).catch(err => {
										uni.hideLoading()
										console.error(err)
									})
								}
							}
						})
					} else {
						this.$u.route('pages/work/apply/schedulemine/edit', {pkid: this.pkid})
					}
				},
				fail: function (res) {
					console.log(res.errMsg);
				}
			})
		},
		methods: {
			loadImg(index){
				console.log(index)
			},
			/* 预览 */
			onOpenDoc(paths) {
				let url = paths
				/* 下载返回临时路径（退出应用失效） */
				this.$refs.lFile.download(url) 
				.then(path=>{
					/* 预览 */
					this.$refs.lFile.open(path);
				});
			},
			initData() {
				let pkid = {pkid:this.pkid}
				this.$u.api.scheduleDetail(pkid).then(res => {
					let _now = new Date()
					let stime=new Date(res.startTime)
					console.log('1', _now.getTime())
					console.log('2', stime.getTime())
							
					res.isStart = _now.getTime() >= stime.getTime()
					res.createTime = this.$u.timeFormat(new Date(res.createTime), 'yyyy年mm月dd日 hh:MM')
					if (res.endTime-res.startTime > 24*60*60*1000) {
						res.startTime = this.$u.timeFormat(new Date(res.startTime), 'yyyy年mm月dd日 周W hh:MM')
						res.endTime = this.$u.timeFormat(new Date(res.endTime), 'yyyy年mm月dd日 周W hh:MM')
					} else {
						res.startTime = this.$u.timeFormat(new Date(res.startTime), 'yyyy年mm月dd日 周W hh:MM')
						res.endTime = this.$u.timeFormat(new Date(res.endTime), 'hh:MM')
					}
					this.jsCount = 0
					if (res.schCollaborators) {
						res.schCollaborators.forEach((v) => {
							if (v.collFlag === 3 || (v.status === 1 && v.collFlag !== 4)) {
								this.jsCount += 1
							}
						})
					}
					res.schContent = decodeURIComponent(res.schContent)
					if (res.schSummary) {
						res.schSummary.schContent = decodeURIComponent(res.schSummary.schContent)
					}
					this.schData = res
				}).catch(err => {
					console.error(err)
				})
				
				// detailSchedule(this.pkid).then(res => {
				// 	let _now = new Date()
				// 	res.isStart = _now.getTime() >= res.startTime
				// 	res.createTime = dateFormat(new Date(res.createTime), 'yyyy年MM月dd日 hh:mm')
				// 	if (res.endTime - res.startTime > 24*60*60*1000) {
				// 		res.startTime = dateFormat(new Date(res.startTime), 'yyyy年MM月dd日 周W hh:mm')
				// 		res.endTime = dateFormat(new Date(res.endTime), 'yyyy年MM月dd日 周W hh:mm')
				// 	} else {
				// 		res.startTime = dateFormat(new Date(res.startTime), 'yyyy年MM月dd日 周W hh:mm')
				// 		res.endTime = dateFormat(new Date(res.endTime), 'hh:mm')
				// 	}
				// 	this.jsCount = 0
				// 	if (res.schCollaborators) {
				// 		res.schCollaborators.forEach((v) => {
				// 			if (v.collFlag === 3 || (v.status === 1 && v.collFlag !== 4)) {
				// 				this.jsCount += 1
				// 			}
				// 		})
				// 	}
				// 	res.schContent = decodeURIComponent(res.schContent)
				// 	if (res.schSummary) {
				// 		res.schSummary.schContent = decodeURIComponent(res.schSummary.schContent)
				// 	}
				// 	this.schData = res
				// 	console.log(res)
				// 	const _userInfo = uni.getStorageSync('USERINFO')
				// 	console.log(_userInfo.token)
				// 	console.log(this.pkid)
				// })
			},
			jsClick(pType) {
				if (pType===1) {
					let _obj = {pkid: this.pkid, isAccept: pType}
					this.$u.api.acceptOrRefuse(_obj).then(res => {
						this.$u.toast('已接受')
						this.initData()
					}).catch(err => {
						console.error(err)
					})
				} else {
					let _menu_list = ['时间冲突，无法参加', '请假了，无法参加', '出差了，无法参加', '其他原因，无法参加']
					uni.showActionSheet({
						itemList: _menu_list,
						success: (res) => {
							// res.tapIndex
							let schObj = {
								schitemId: this.pkid,
								schContent: encodeURIComponent(_menu_list[res.tapIndex]),
								fileUrls: []
							}
							let pkid = { pkid:this.pkid }
							this.$u.api.addFeedBack(schObj).then(res => {
								let pkid = { pkid:this.pkid }
								this.$u.api.setDone(pkid).then(res => {
									let acpObj = {pkid: this.pkid, isAccept: pType}
									this.$u.api.acceptOrRefuse(acpObj).then(res => {
										this.$u.toast('已拒绝')
										this.initData()
									}).catch(err => {
										console.error(err)
									})
								}).catch(err => {
									console.error(err)
								})
							}).catch(err => {
								console.error(err)
							})
							
						},
						fail: function (res) {
							console.log(res.errMsg);
						}
					})
				}
			},
			fkClick() {
				this.$u.route('pages/work/apply/schedulemine/feedback', {pkid: this.pkid})
			},
			previewImage(idx, list) {
				let preview = [];
				for (let i = 0, len = list.length; i < len; i++) {
					preview.push(list[i].url||list[i]);
				}
				uni.previewImage({
					current: idx,
					urls: preview
				});
			},
			fkDelClick(pPkid) {
				let pkid = { pkid:pPkid }
				this.$u.api.removeFeedBack(pkid).then(res => {
					this.$u.toast('删除成功')
					this.initData()
				}).catch(err => {
					console.error(err)
				})
			},
			doneClick() {
				uni.showModal({
					title: '提示',
					content: '确定完成日程吗？',
					success: (res) => {
						if (res.confirm) {
							let pkid = { pkid:this.pkid }
							this.$u.api.setDone(pkid).then(res => {
								this.$u.toast('提交成功')
								this.initData()
							}).catch(err => {
								console.error(err)
							})
						}
					}
				})
			},
			summryClick() {
				this.$u.route('pages/work/apply/schedulemine/summry', {pkid: this.pkid})
			},
			toCollList() {
				this.$u.route('pages/work/apply/schedulemine/colllist', {pkid: this.pkid})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.detail-page {
		padding: 0px 0px;
		background-color: #F7F8FA;
		height: 100%;
	}
	.coll-lay {
		background-color: #ffffff;
	}
	.coll-row {
		padding: 16px;
		border-bottom: 1px solid #eee;
		color: #999;
		text {
			padding-right: 8px;
			font-size: 18px;
			font-weight: bold;
			color: #0091EA;
		}
	}
	.sch-content {
		padding: 16px;
		min-height: 18px;
		font-size: 18px;
		line-height: 18px;
		word-break: break-all;
		border-bottom: 1px solid #eee;
	}
	.sch-time {
		padding: 0px 16px;
		.sch-time-item {
			padding: 16px;
			text {
				color: #999;
			}
			border-bottom: 1px solid #eee;
		}
	}
	.coll-list, .fk-list {
		display: flex;
		padding: 16px;
		justify-content: space-between;
		border-bottom: 8px solid #eee;
		.title {
			padding-left: 2px;
			font-size: 18px;
			font-weight: bold;
		}
		.rtext {
			color: #999;
		}
	}
	.fk-list {
		border-bottom: none;
		.fk-btn {
			color: #0070C4;
			font-size: 14px;
		}
	}
	.fk-lay {
		padding: 0px 16px 50px 16px;
		margin-bottom: 50px;
	}
	.fk-item {
		margin-bottom: 10px;
		border-radius: 15px;
		background: #ffffff;
		padding:  10px 0px;
		.fk-from {
			display: flex;
			padding:  0px 16px;
			justify-content: space-between;
			.fk-time {
				font-size: 12px;
				color: #999;
			}
		}
		.fk-content {
			padding: 8px 16px;
			word-break: break-all;
		}
		.fk-del {
			height: 30px;
			line-height: 35px;
			border-top: 1px solid #eeeeee;
			text-align: center;
			font-size: 14px;
			color: #0070C4;
		}
	}
	.no-fk {
		padding: 16px;
		text-align: center;
		color: #CCCCCC;
	}
	.js-bar {
		position: fixed;
		display: flex;
		box-sizing: border-box;
		width: 100%;
		padding: 8px;
		bottom: 0;
		border-top: 1px solid #eee;
		background-color: #FFFFFF;
		.js-btn {
			width: 33%;
		}
	}
	.file-list {
		display: flex;
		padding: 8px 16px;
		flex-wrap: wrap; 
		image {
			margin-right: 16px;
			margin-bottom: 16px;
			width: 162rpx;
			height: 162rpx;
			// background: url(../../../../static/image/loading.gif) no-repeat center center;
			background-size:15px 15px;
		}
	}
	.done-bar {
		margin: 25px 0 10px 0;
		padding: 16px;
	}
	.summry-content {
		padding: 16px;
		border-bottom: 1px solid #eee;
		.title {
			font-size: 18px;
			font-weight: bold;
			text {
				padding-left: 8px;
				font-size: 12px;
				color: #999;
			}
		}
		.content {
			word-break: break-all;
		}
	}
	.addBox{
		width: 100%;
		height: 100px;
		text-align: center;
		//background: url(../../static/img/upload.png) no-repeat center center;		
		position: relative;
		border: 1px dashed #1890FF;
		margin: 0 auto;
		border-radius: 8px;
		background-color: #FAFAFA;
	}
	
	.uploadtitle{
		position: absolute;
		bottom: 12rpx;
		left: 0;
		right: 0;
	}
	.file-row {
		margin: 8px 0;
		padding: 16px;
		background-color: #FFFFFF;
	}
	.filesBox{
		margin-top: 28rpx;
		width: 100%;
		display: inline-block;
	}
	
	.fileslist{
		margin-top: 8px;
		
	}
	
	.upfileico{
		width: 40rpx;
		height: 40rpx;
		display: inline-block;
		float: left;
		margin-right: 2px;
	}
	 
	.upname{
		font-size: 26rpx;
		line-height: 40rpx;
		color: #333333;
		width: 90%;
		display: inline-block;
	}
	
	.upclose{
		width: 40rpx;
		height: 40rpx;
		display: inline-block;
		float: right;
	}
	.finish_btn {
		border-radius: 45px;
		align-self: center;
		height: 45px;
		line-height: 45px;
		font-size: 16px;
		width: 92vw;
		background-image: linear-gradient(to right, #22ABEC, #0071C5);
		justify-content: center;
		align-items: center;
	}
</style>
